package com.ip.student.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ip.common.constant.Result;
import com.ip.common.enums.CodeEnum;
import com.ip.common.utils.StrUtils;
import com.ip.user.models.common.annotation.RequiresPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.ip.student.models.service.IBaseClassService;
import com.ip.student.models.bo.BaseClassSaveBO;
import com.ip.student.models.bo.BaseClassBO;
import com.ip.student.models.dto.BaseClassDTO;

/**
 * 班级信息控制器
 *
 * @author 张景奇
 * @Date 2025-10-25 10:55:52
 */

@RestController
@RequestMapping("/cms/baseClass")
@Slf4j
@Api(value = "BaseClassController",tags = "班级信息接口")
public class BaseClassController {

    @DubboReference
    private IBaseClassService baseClassService;

    @ApiOperation(value = "添加班级信息",notes = "添加班级信息")
    @PostMapping(value = "/insert")
    @RequiresPermission("base:class:menu")
    public Result insert(@RequestBody BaseClassSaveBO saveBO) {
        log.info("接收到的班级信息: {}", saveBO);
        boolean flag = baseClassService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存班级信息",notes = "保存班级信息")
    @RequiresPermission("base:class:menu")
    public Result save(@RequestBody BaseClassSaveBO saveBO){
        if(saveBO.getClassId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改班级信息",notes = "修改班级信息")
    @PostMapping("/update")
    @RequiresPermission("base:class:menu")
    public Result update(@RequestBody BaseClassSaveBO saveBO){
        BaseClassDTO dto = new BaseClassDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setClassId(saveBO.getClassId());
        boolean flag = baseClassService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除班级信息",notes = "删除班级信息")
    @PostMapping("/delete")
    @RequiresPermission("base:class:menu")
    public Result delete(@RequestBody Map<String, Object> params){
        Object idObj = params.get("id");
        if (idObj == null) {
            // 尝试获取classId参数
            idObj = params.get("classId");
        }
        if (idObj == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Long id = Long.valueOf(idObj.toString());
        boolean flag = baseClassService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }


    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除班级信息",notes = "批量删除班级信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    @RequiresPermission("base:class:menu")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        baseClassService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    @RequiresPermission("base:class:query")
    public Result<BaseClassDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        BaseClassDTO dto = baseClassService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "班级信息列表",notes = "班级信息列表")
    @PostMapping("/queryPageList")
    @RequiresPermission("base:class:query")
    public Result<Page<BaseClassDTO>> queryPageList(@RequestBody BaseClassBO bo){
        Wrapper<BaseClassDTO> wrapper = new EntityWrapper<>();
        
        // 添加查询条件
        if (bo.getClassId() != null) {
            wrapper.eq("class_id", bo.getClassId());
        }
        if (bo.getSchoolId() != null) {
            wrapper.eq("school_id", bo.getSchoolId());
        }
        if (bo.getClassCode() != null && !bo.getClassCode().isEmpty()) {
            wrapper.eq("class_code", bo.getClassCode());
        }
        if (bo.getClassName() != null && !bo.getClassName().isEmpty()) {
            wrapper.like("class_name", bo.getClassName());
        }

        Page<BaseClassDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = baseClassService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}